<?php
class Sprint extends CI_Controller {

	function __construct(){
        parent::__construct();
        
        $this->load->model('sprint_model');
        $this->load->helper(array('form','url'));
        $this->load->library('form_validation');
    }
    function index(){
        $this->load->view('sprintsList');
    } 
    
	function getSprints(){
		$page = $this->input->post('page');
		$limit = $this->input->post('rows');
		$sidx = $this->input->post('sidx');
		$sord = $this->input->post('sord');
		
		if(!$sidx) $sidx =1;
		
		$count = $this->sprint_model->getSprintsCount();

		if( $count > 0 ) {
			$total_pages = ceil($count/$limit);
		} else {
			$total_pages = 0;
		}
		if ($page > $total_pages) $page=$total_pages;
		$start = $limit*$page - $limit; // do not put $limit*($page - 1)
		
		$responce->page = $page;
		$responce->total = $total_pages;
		$responce->records = $count;
		
		$sprints = $this->sprint_model->getSprints($sidx, $sord, $start , $limit); 
		
		for($i=0; $i< sizeof($sprints); $i++) {
			$responce->rows[$i]['id'] = $sprints[$i]['id'];
			$responce->rows[$i]['cell'] = array($sprints[$i]['id'], 
												$sprints[$i]['start_date'], 
												$sprints[$i]['end_date'],
												$sprints[$i]['status']);
		}
     	
		echo json_encode($responce);
	}
	
	function edit(){
		$this->load->model('sprint_model');
		if ($_POST['oper'] == 'add'){ 
			$d1=$_POST['start_date'];
			$d2=$_POST['end_date'];
			$start_date = date('Y-m-d', strtotime($d1));
			$end_date = date('Y-m-d', strtotime($d2));
			
			$name = date($d1 . ' - ' . $d2);
			$status = $_POST['status'];
			if($status == 'Closed'){
				//cannot create a closed sprint
				//show_error('message!!!!',500);
				
				return;
			}
			if($this->sprint_model->controlInProgress($status)==true){
				//cannot be more than one sprint in progress
				return;
			}
			$this->sprint_model->newSprint($start_date,$end_date, $name, $status);
        }else if ($_POST['oper'] == 'del'){
        	$sprintId = $_POST['id'];
        	$this->sprint_model->delete($sprintId);
        }else if ($_POST['oper'] == 'edit'){
        	$sprintId = $_POST['id'];
        	$d1=$_POST['start_date'];
			$d2=$_POST['end_date'];
			$start_date = date('Y-m-d', strtotime($d1));
			$end_date = date('Y-m-d', strtotime($d2));
			
			$name = date($d1 . ' - ' . $d2);
			$status = $_POST['status'];
        	if($this->sprint_model->controlInProgress($status)==true){
				//cannot be more than one sprint in progress
				return;
			}
			$this->sprint_model->update($sprintId,$start_date, $name,$end_date,$status);
        }
	}
    /*
    public function get_json_sprints_autocomplete(){
    	$id_project = $this->session->userdata('current_project');
		$sprints = $this->sprint_model->get_project_sprints($id_project);
		$responce = null;
    	for($i=0; $i< sizeof($sprints); $i++){
    		$start_date = date('d/m/Y', strtotime($sprints[$i]['start_date']));
			$end_date = date('d/m/Y', strtotime($sprints[$i]['end_date']));
    		
    		$responce[$i]['value'] = $sprints[$i]['id'];
			$responce[$i]['label'] = $start_date . ' - ' . $end_date;
		}
		echo json_encode($responce);
    }
 */
	
	public function get_json_sprints_autocomplete(){
		$term = $this->input->get('term');
		$sprints = $this->sprint_model->get_like_sprints($term);
		
		$auto_complete_sprints = array();
		
		for($i=0; $i< sizeof($sprints); $i++) {
			$auto_complete_sprints[$i]['id'] 	= $sprints[$i]['id'];
			$auto_complete_sprints[$i]['value'] = $sprints[$i]['name'];
		}
		
		echo json_encode($auto_complete_sprints);
	}
}
?>